<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Biometric extends User_Controller
{
	
	
    public function __construct()
    {
        parent::__construct();
        if(isset($this->session) && !$this->session->userdata('isLogged')) {
        	redirect('/');
        }
        $this->requestManager = RequestManager::getInstance();
        $this->settings = $this->config->item('settings');
        $this->load->helper(array('StringUtils','DateUtils','FileUtils'));

    }

	public function index()
	{	
		
		$data = array();
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."biometric/fileList/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		if($return->message == 'success'){
			$data['lists'] = $return->data;
		}
		
		$this->load->view('header');
		$this->load->view('index', $data);
		$this->load->view('footer');

	}
	
	public function upload(){
		$data = array();
		$error = false;
		if($this->input->post()){
			//echo "Asdfads";
			///$filename = str_replace(' ','_',$_FILES['name']);
			$config['upload_path'] = './timesheets/';
			$config['allowed_types'] = 'xls';
			$config['max_size']		= '2000';
			$config['file_name']  = substr($_FILES['userfile']['name'], 0, -4).'-'.time();

			$this->load->library('upload', $config);
			if(!$this->getValidFilename($_FILES['userfile']['name'])) {
				$error = true;
			}
	
			if($error == false){
				if ( ! $this->upload->do_upload())
				{			
					$error = array('error' => $this->upload->display_errors());
					$data['message'] = $error;
				} else {
					// save to dbase...
					$sess = $this->session->all_userdata();
					$admin_id = $sess['user_id'];
	
					$info = array('upload_data' => $this->upload->data());
					$this->requestManager->setData(array('file_name'=>$info['upload_data']['file_name'],
					'orig_file_name'=>$info['upload_data']['client_name'],
					'user_id'=>$admin_id,'file_size'=>$info['upload_data']['file_size']));
					$this->requestManager->setSendType("post");
					$this->requestManager->setAccessPath($this->settings['api_path']."biometric/upload/" .$this->settings['client_key']);
					$return = $this->requestManager->send();
					if(isset($return)){
						if($return->message == 'success'){
							// copy xcel file to api timesheets..
							$src = '/timesheets/'. $info['upload_data']['file_name'];
							$dest = $this->settings['api_root_path'] . $this->settings['api_doc_path'] ."/{$info['upload_data']['file_name']}";
							if (FileUtils::stream_copy($src, $dest )) {
								redirect('biometric/index');
							}
							
							
						} else {
							$data['message']="error";
						}
					}
					
				}
			} else {
				$error = array('error' => $this->upload->display_errors());
				$data['message'] = "Make sure that the file will contain only 1 ('.'). ";
			}
			
			
		}
		//$data['info'] = $info;
		//var_dump($_FILES);
		$this->load->view('header');
		$this->load->view('index', $data);
		$this->load->view('footer');
		
	}
	
	public function view($id){
		$data = array();
		$sess = $this->session->all_userdata();
		$admin_id = $sess['user_id'];
		$this->requestManager->setData(array($id,$admin_id));
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."biometric/view/" .$this->settings['client_key']);
		$return = $this->requestManager->send();

		if(isset($return)){
			if($return->message == 'success'){
				$data['bio'] = $return->data;
			} else {
				$data['message']="error";
			}
		}
		
		$this->load->view('lightbox_header');
		$this->load->view('view',$data);
		$this->load->view('lightbox_footer');
		
	}
	
	public function load($id){
		$sess = $this->session->all_userdata();
		$admin_id = $sess['user_id'];
		$this->requestManager->setData(array($id,$admin_id));
		$this->requestManager->setSendType("rest");
		$this->requestManager->setAccessPath($this->settings['api_path']."biometric/load/" .$this->settings['client_key']);
		$return = $this->requestManager->send();
		//var_dump($return);
		if(isset($return)){
			echo $return->message;
		} else {
			echo "failed";
		}
		die();
	}
	
	private function getValidFilename($filename){
		
		if($filename != "") {
			$names = explode(".",$filename);
			if(count($names) > 2){
				return false;
			}
		}
		
		return true;
	}
}